import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;


public class Ex4_3 {
	
	
	public Ex4_3() {}	
	
	public static void main(String[] args) {
		Ex4_3 exe = new Ex4_3();
		Integer k = 12;
		String text = exe.
					getFirstLine("C:\\Users\\josemfer\\Downloads\\DNAs.txt");
		exe.DeBruijn(k, text);
		
	}
	
	
	
	private void DeBruijn(Integer k, String text) {
		HashMap<String,ArrayList<String>> graph = 
							new HashMap<String,ArrayList<String>>();
		for (int i=0;i<=text.length()-k;i++) {
			String kmer = text.substring(i,i+k-1);
			String nkmer = text.substring(i+1,i+k);
			ArrayList<String> list = graph.get(kmer);
			if (list == null) {
				ArrayList<String> newList = new ArrayList<String>();
				newList.add(nkmer);
				graph.put(kmer, newList);
			} else {
				if (!list.contains(nkmer)) {
					list.add(nkmer);
				}
			}			
		}
		
		for(Entry entry : graph.entrySet()) {
			String kmer = (String) entry.getKey();
			ArrayList<String> list =  (ArrayList<String>) entry.getValue();
			System.out.print(kmer + " -> ");		
			int i = 0;
			for (String km : list) {				
				if (i > 0) {
					System.out.print(",");
				}
				System.out.print(km);
				i++;
			}
			System.out.print("\n");		
		}
		
	}
	
	
	private void writeFile(String path, String text) {
		try
		{
			String file_name = path;
			FileWriter file = new FileWriter(file_name);
			BufferedWriter out = new BufferedWriter (file);			
			out.write(text);
			out.close();
		}
		catch (IOException e)
		{}
	}
	
	public String getFirstLine(String file) {		
		String line = null;		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			line = br.readLine();			
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return line;	
	}

}
